/**
 * 
 */
package ar.edu.unq.svnpermission.ui.model;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import ar.edu.unq.svnpermission.domain.Folder;
import ar.edu.unq.svnpermission.ui.exceptions.FolderAlreadyExist;
import ar.unq.edu.svnpermission.homes.FolderHome;

/**
 * Cubre las funcionalidades de creacion de una nueva carpeta
 * @author nicolas
 */
public class NewFolder extends UseCase {

	
	private static final long serialVersionUID = 1L;
	private Folder folder = new Folder();
	
	public NewFolder()
	{
		super();
	}
	
	public NewFolder(Folder parentFolder)
	{
		super();
		this.getFolder().setParent(parentFolder);
	}
	
	/**
	 * Crea una nueva carpeta
	 * @exception Lanza una @ref FolderAlreadyExist en el caso de que exista una 
	 * con el mismo path
	 * @author nicolas
	 */
	public void create()
	{
		//Si la carpeta existe, lanzo una exepción de negocio
		//if(this.exist(this.getFolder()))
			//throw new FolderAlreadyExist();
		

		this.getFolder().getParent().addChildFolder(this.getFolder());
		
		//Persisto la carpeta en el repositorio
		this.getHome(Folder.class, FolderHome.class).update(this.getSuperParentFolder());
	}
	
	
	private Folder getSuperParentFolder()
	{
		Folder parentFolder = this.getFolder().getParent();
		
		while(parentFolder.getParent()!=null)
		{
			parentFolder = parentFolder.getParent();
		}
		return parentFolder;
	}
	
	/**
	 * Determina si una carpeta existe en el repositorio
	 * @param folder La carpeta buscada
	 * @return True en el caso que exista. False en caso contrario
	 */
	private Boolean exist(Folder folder)
	{
		return !this.getHome(Folder.class, FolderHome.class).searchByExample(folder).isEmpty();
	}
	
	private Folder getFolder() {
		return folder;
	}

	private void setFolder(Folder folder) {
		this.folder = folder;
	}	
	

	/****************************************************************************
	 * ACCESSORS*
	 * *************************************************************************/
	public String getName() {
		return this.getFolder().getName();
	}
	public void setName(String name) {
		this.getFolder().setName(name);
	}
	public String getComments() {
		return this.getFolder().getComments();
	}
	public void setComments(String comments) {
		this.getFolder().setComments(comments);
	}
	public void setParentFolder(Folder parentFolder)
	{
		this.getFolder().setParent(parentFolder);
	}
	public Folder getParentFolder()
	{
		return this.getFolder().getParent();
	}
		
}
